/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.ControlEscolar.Services;

import com.ControlEscolar.Beans.Exam;
import com.ControlEscolar.DAOs.DAOException;
import com.ControlEscolar.DAOs.StudentDAO;
import com.ControlEscolar.Beans.Student;
import com.ControlEscolar.Exceptions.AlreadyExistExp;
import com.ControlEscolar.Exceptions.ServiceExp;
import com.ControlEscolar.Exceptions.DeleteExp;
import com.ControlEscolar.Validations.ValidationStudent;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;


/**
 *
 * @author Bere
 */
  public class StudentService {

    StudentDAO studentDAO= new StudentDAO();
    ValidationStudent validationStudent=new ValidationStudent();
    private static final String FILE_NAME_STUDENT="Reticula de ";
    private static final int COLUMS_NUMBER_STUDENT_DATA=3;
    ExamService examService = new ExamService();

    public void createStudent(Student student) throws ServiceExp, AlreadyExistExp {
      try{
        if(validationStudent.isExistStudent(student.getStudentKey(),student.getCURP())){
          throw new AlreadyExistExp("YA EXISTE ESE DATO");
        }
        else
            studentDAO.add(student);}
      catch(DAOException e){
            throw new ServiceExp();
        }
    }
    public void deleteStudent(String studentKey) throws ServiceExp, DeleteExp {
      try{
        if(validationStudent.isStudentEnrolled(studentKey)){
            throw new DeleteExp("EL ALUMNO ESTA INSCRITO");
        }
        else
            studentDAO.delete(studentKey);}
      catch(DAOException e){
            throw new ServiceExp();
        }

    }
    public void updateStudent(Student student) throws ServiceExp {
       try{
        studentDAO.update(student);}
       catch(DAOException e){
            throw new ServiceExp();
        }

    }

    public Student getStudent(String studentKey) throws ServiceExp{
        try {
            return studentDAO.queryByKey(studentKey);
        } catch (DAOException ex) {
            throw new ServiceExp();
        }
    }

    public ArrayList <Student> getStudentList()throws ServiceExp{
        try {
            return studentDAO.query();
        } catch (DAOException ex) {
            ex.printStackTrace();
            throw new ServiceExp();
        }

    }
    public void printStudentData(String studentKey){
        try {
            Student studentData = getStudent(studentKey);

            Document PdfDocument = new Document(PageSize.LETTER.rotate());

            PdfPTable StudentDataTable = new PdfPTable(COLUMS_NUMBER_STUDENT_DATA);

            ArrayList<Exam> examDataList = examService.getStudentLastExams(studentKey);
            
            PdfWriter.getInstance(PdfDocument, new FileOutputStream(FILE_NAME_STUDENT+studentData.getStudentKey()+".pdf"));
            
            PdfDocument.open();

            PdfDocument.add(new Paragraph("Matricula:" + studentData.getStudentKey()));
            PdfDocument.add(new Paragraph("Nombre:" + studentData.getName() + studentData.getFirstLastName()));
            PdfDocument.add(new Paragraph(" "));
            Phrase phrase = new Phrase("Materia");

            defaultColumnsStudent(StudentDataTable);

            PdfPCell infoCell = new PdfPCell(phrase);
            

            for (int index = 0; index < examDataList.size(); index++) {
                StudentDataTable.addCell("" + examDataList.get(index).getSubjectKey());
                StudentDataTable.addCell("" + examDataList.get(index).getID_Professor());
                StudentDataTable.addCell("" + examDataList.get(index).getExamGrade());
            }
            
            StudentDataTable.addCell(infoCell);
            PdfDocument.add(StudentDataTable);
            PdfDocument.close();

        } catch (ServiceExp ex) {
            Logger.getLogger(StudentService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (DocumentException ex) {
            Logger.getLogger(StudentService.class.getName()).log(Level.SEVERE, null, ex);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(StudentService.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public void defaultColumnsStudent(PdfPTable studentDataTable){
            studentDataTable.addCell("Materia");
            studentDataTable.addCell("Profesor");
            studentDataTable.addCell("Calificaciones");
    }
  }

